package com.donghuayuan.kotlincar

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.donghuayuan.kotlincar.adapter.BrandAdapter
import com.donghuayuan.kotlincar.bean.BrandListBean
import com.donghuayuan.kotlincar.databinding.ActivityBrandListBinding
import com.donghuayuan.kotlincar.bean.Result
import com.donghuayuan.kotlincar.net.CarApi
import com.donghuayuan.kotlincar.net.RetrofitUtils
import com.google.gson.Gson
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class BrandListActivity : AppCompatActivity() {

    var mList:MutableList<Result> = mutableListOf()
    val binding:ActivityBrandListBinding by lazy {
        DataBindingUtil.setContentView(this@BrandListActivity,R.layout.activity_brand_list)
    }
    val adapter:BrandAdapter by lazy {
        BrandAdapter(this@BrandListActivity,mList)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val id = intent.getStringExtra("bean")

        binding.adapter = adapter
        loadData(id)


    }

    private fun loadData(id: String?) {
        val api = RetrofitUtils.getApi(CarApi.base_url, CarApi::class.java)
        val call = api.getBrandList(id+"", "key")
        call?.enqueue(object :Callback<BrandListBean>{
            override fun onResponse(call: Call<BrandListBean>, response: Response<BrandListBean>) {
                println("BrandListActivity请求成功")

                val bean = response.body()
                binding.title = id
                val gson= Gson()
                println("请求成功"+gson.toJson(bean))
                adapter.addList(bean?.result)
            }

            override fun onFailure(call: Call<BrandListBean>, t: Throwable) {
                println("BrandListActivity请求失败")
            }
        })
    }
}